<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2017/8/20
 * Time: 下午8:01
 */
class AuctionController extends AdminBaseController {

    public function index(){
        $this->setTitle('寄卖行数据监控');

        $filter = new PubFilter('pl');
        $grid   = new PubGrid();

        $filter->addServerFilter(false)
            ->addFilter('time_start', 'input', '时间', array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('time_end', 'input', '至', array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('record_id', 'input', '交易ID')
            ->addFilter('sell_id', 'input', '卖家ID')
            ->addFilter('buy_id', 'input', '买家ID')
            ->addFilter('item_id', 'input', '物品ID')
        ;

        $params = $filter->get();

        $header = AuctionService::indexHeader();
        $data   = AuctionService::indexData($params);

        if($filter->isExport()) {
            $filename = LibExcel::getFilename('', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'name'  => '寄卖行数据监控',
                'order' => array('RecDate', 'desc')
            ));

            $this->out['filter']    = $filter->show();
            $this->out['grid']      = $grid->show();
        }
    }

    public function action(){
        $strData    = $this->R('data');
        $type       = $this->R('type');
        $serverId   = $this->R('server');

        $data       = json_decode(base64_decode($strData), true);

        $title = array(
            'cancel'    => '交易取消',
            'frozen'    => '材料冻结',
            'close'     => '屏蔽寄售',
            'bond'      => '保证金扣除',
            'banBuy'    => '封禁买家',
            'banSell'   => '封禁卖家',
        );

        $this->out['type']      = $type;
        $this->out['server']    = $serverId;
        $this->out['title']     = $title[$type];
        $this->out['data']      = $data;

        $this->layout = 'auction_action';
        $this->tpl = 'auction/action_' . $type;
    }

    // 交易取消
    public function actionCancel(){
        $server = $_POST['server'];

        $params = array(
            'roleID'            => $_POST['roleID'],
            'recordItemID'      => $_POST['recordItemID'],
            'recPrice'          => $_POST['recPrice'],
            'getStone'          => $_POST['getStone'],
            'deductionStone'    => $_POST['deductionStone'],
        );

        $refer = $_SERVER['HTTP_REFERER'];
        if(empty($refer)){
            $refer = PubFunc::buildUrl('/oss/?r=auction/index');
        }

        $result = AuctionService::actionCancel($server, $params);
        if($result['state']){
            $this->showMessage('操作成功', $refer);
        }else{
            $this->showMessage($result['msg'], $refer);
        }
    }

    // 材料冻结
    public function actionFrozen(){
        $server = $_POST['server'];

        $params = array(
            'roleID'        => $_POST['roleID'],
            'itemID'        => $_POST['itemID'],
            'frozenTime'    => strtotime($_POST['frozenTime']),
            'itemType'      => $_POST['itemType'],
        );

        $refer = $_SERVER['HTTP_REFERER'];
        if(empty($refer)){
            $refer = PubFunc::buildUrl('/oss/?r=auction/index');
        }

        $result = AuctionService::actionFrozen($server, $params);
        if($result['state']){
            $this->showMessage('操作成功', $refer);
        }else{
            $this->showMessage($result['msg'], $refer);
        }
    }

    // 屏蔽寄售
    public function actionClose(){
        $server = $_POST['server'];

        $params = array(
            'roleID'        => $_POST['roleID'],
            'endTime'       => strtotime($_POST['endTime']),
            'status'        => 1
        );

        $refer = $_SERVER['HTTP_REFERER'];
        if(empty($refer)){
            $refer = PubFunc::buildUrl('/oss/?r=auction/index');
        }

        $result = AuctionService::actionClose($server, $params);
        if($result['state']){
            $this->showMessage('操作成功', $refer);
        }else{
            $this->showMessage($result['msg'], $refer);
        }
    }

    // 保证金扣除
    public function actionBond(){
        $server = $_POST['server'];

        $params = array(
            'roleID'        => $_POST['roleID'],
            'days'          => $_POST['days'],
            'bondStone'     => $_POST['bondStone'],
            'itemID'        => $_POST['itemID'],
            'itemType'      => $_POST['itemType'],
            'price'         => $_POST['price'],
        );

        $refer = $_SERVER['HTTP_REFERER'];
        if(empty($refer)){
            $refer = PubFunc::buildUrl('/oss/?r=auction/index');
        }

        $result = AuctionService::actionBond($server, $params);
        if($result['state']){
            $this->showMessage('操作成功', $refer);
        }else{
            $this->showMessage($result['msg'], $refer);
        }
    }

}